import md5 from 'js-md5';

/*
 * 获取处理后的时间戳
 */
export const timestamp = () => {
	let stamp = new Date().getTime()/1000;
	//let timestamp = (stamp^(stamp/1000000)) * 2;
	return stamp;
}

/**
 * 判断是否微信浏览器
 */
export const isWeixinBrowser = () => {
	var ua = window.navigator.userAgent.toLowerCase();
    return (/micromessenger/.test(ua));
}

/**
 * 判断是否QQ浏览器
 */
export const isQQBrowser = () => {
	var ua = window.navigator.userAgent.toLowerCase();
    return (/mqqbrowser/.test(ua));
}

/**
 * 获取浏览器名称
 */
export const browserName = () => {
	var ua = window.navigator.userAgent;
	var r = new Array();
    var browser = '';
    if (r = ua.match(/FireFox\/([^\s]+)/ig)) {
        browser = 'Mozilla FireFox';
    } else if (r = ua.match(/Edge([\d]*)\/([^\s]+)/gi)) {
        browser = 'Microsoft Edge';
    } else if (r = ua.match(/Maxthon([\d]*)\/([^\s]+)/ig)) {
        browser = 'Maxthon';
    } else if (r = ua.match(/BIDUBrowser([\d]*)\/([^\s]+)/ig)) {
        browser = 'BIDUBrowser';
    } else if (r = ua.match(/UBrowser([\d]*)\/([^\s]+)/ig)) {
        browser = 'UCBrowser';
    } else if (r = ua.match(/UCBrowser([\d]*)\/([^\s]+)/ig)) {
        browser = 'UCBrowser';
    } else if (r = ua.match(/MetaSr/ig)) {
        browser = 'MetaSr';
    } else if (r = ua.match(/2345Explorer/ig)) {
        browser = '2345Explorer';
    } else if (r = ua.match(/2345chrome/ig)) {
        browser = '2345chrome';
    } else if (r = ua.match(/LBBROWSER/ig)) {
        browser = 'LBBrowser';
    } else if (r = ua.match(/MicroMessenger\/([^\s]+)/ig)) {
        browser = 'MicroMessenger';
    } else if (r = ua.match(/QQBrowser\/([^\s]+)/ig)) {
        browser = 'QQBrowser';
    } else if (r = ua.match(/QQ\/([^\s]+)/ig)) {
        browser = 'QQBrowser';
    } else if (r = ua.match(/MiuiBrowser\/([^\s]+)/ig)) {
        browser = 'MiuiBrowser';
    } else if (r = ua.match(/Chrome([\d]*)\/([^\s]+)/ig)) {
        browser = 'Chrome';
    } else if (r = ua.match(/safari\/([^\s]+)/ig)) {
        browser = 'Safari';
    } else if (r = ua.match(/Opera[\s|\/]([^\s]+)/ig)) {
        browser = 'Opera';
    } else if (r = ua.match(/Trident\/7.0/gi)) {
        browser = 'Internet Explorer';
    } else if (r = ua.match(/MSIE\s([^\s|;]+)/gi)) {
        browser = 'Internet Explorer';
    } else {
        browser = 'Other';
    }
    return browser;
}

/**
 * 获取客户端类型
 */
export const clientType = () => {
	return 3;  // 1 IOS 2 安卓 3 web 4 公众号H5 5 小程序 6 QQ
}

/**
 * 获取设备ID
 */
export const deviceId = () => {
	let device = browserName() + browserVersion() + clientType() + window.navigator.userAgent.toLowerCase();
	return md5(device);
}

/**
 * 获取请求ID
 */
export const requestId = () => {
	return UUID();
}

export function UUID(){
	var str=[];
	var Chars='0123456789abcdefghijklmnopqrstuvwxyz';
	for(var i=0;i<36;i++){
		str[i]=Chars.substr(Math.floor(Math.random()*16),1)
	}
	str[0]=str[8]=str[13]=str[18]=str[23];
	return str.join("");
}

//字符串转字符流
export const string2ArrayBuffer = (s) => {
    if (typeof ArrayBuffer !== 'undefined') {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    } else {
        var buf = new Array(s.length);
        for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
}

/**
 * 获取浏览器版本
 */
export const browserVersion = () => {
	var ua = window.navigator.userAgent;
	var r = new Array();
    var sn = '';
    if (r = ua.match(/FireFox\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/Edge([\d]*)\/([^\s]+)/gi)) {
    	var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/Maxthon([\d]*)\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/BIDUBrowser([\d]*)\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/UBrowser([\d]*)\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/UCBrowser([\d]*)\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/MetaSr/ig)) {
        sn = '';
    } else if (r = ua.match(/2345Explorer/ig)) {
        sn = '';
    } else if (r = ua.match(/2345chrome/ig)) {
        sn = '';
    } else if (r = ua.match(/LBBROWSER/ig)) {
        sn = '';
    } else if (r = ua.match(/MicroMessenger\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/QQBrowser\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/QQ\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/MiuiBrowser\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/Chrome([\d]*)\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/safari\/([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/Opera[\s|\/]([^\s]+)/ig)) {
        var r1 = r[0].split("/");
        sn = r1[1];
    } else if (r = ua.match(/Trident\/7.0/gi)) {
        sn = '11';
    } else if (r = ua.match(/MSIE\s([^\s|;]+)/gi)) {
        sn = r[0];
    } else {
        sn = '';
    }
    return sn;
}
